package acm;

import java.util.Scanner;

public class a17 {
    public static void main(String[] args) {
        int[] a = new int[300];
        Scanner in = new Scanner(System.in);
        int count = in.nextInt();
        String [] n = new String[count];
        in.nextLine();
        for (int j=0;j<count;j++){
            n[j]=in.nextLine();
        }
        for (int x=0;x<n.length;x++) {
            int xb = 0;
            long m = Long.parseLong(n[x],16);
            while (m != 0) {
                a[xb] = (int) (m % 8);
                m = m / 8;
                xb++;
            }
            for (int i = xb - 1; i >= 0; i--) {
                System.out.print(a[i]);
            }
            System.out.println();
        }
    }
}
